package com.dd.ent.car.common.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.JsonRequest;
import com.google.gson.JsonObject;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by aqu-spring on 16/8/6.
 */
public class BaseJsonObjectRequest extends Request<JSONObject> {
    private static final int TIME_OUT = 30000;

    private static DefaultRetryPolicy defaultRetryPolicy = new DefaultRetryPolicy(TIME_OUT, 0,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    private Map<String, String> mMap = new HashMap<>();
    private Response.Listener<JSONObject> listener;

    public BaseJsonObjectRequest(String url, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = listener;
    }

    public BaseJsonObjectRequest(String url, Map<String, String> params, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener) {
        //// TODO: 16/8/6  parse  the parms to jsonobject
        super(Method.POST, url, errorListener);
        this.listener = listener;
        //this.mMap = encodedParams(params);
        this.mMap = params;

    }


    //mMap是已经按照前面的方式,设置了参数的实例
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return mMap;
    }


    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse networkResponse) {
        try {
            JSONObject jsonObject = new JSONObject(new String(networkResponse.data,
                    "UTF-8"));
            return Response.success(jsonObject,
                    HttpHeaderParser.parseCacheHeaders(networkResponse));
        } catch (Exception e) {
            return Response.error(new ParseError(e));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        if (listener != null) {
            listener.onResponse(response);
        }
    }

    // 拼接Post请求参数  to requestbody
    private static Map<String, String> encodedParams(Map<String, String> params) {
        if (params != null && params.size() > 0) {
            String paramsEncoding = "UTF-8";
            Map<String, String> encodedParams = new HashMap<>();
            try {
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    if (entry.getValue() == null) {
                        encodedParams.put(URLEncoder.encode(entry.getKey(), paramsEncoding), entry.getValue());
                        continue;
                    }
                    encodedParams.put(URLEncoder.encode(entry.getKey(), paramsEncoding), URLEncoder.encode(entry.getValue(), paramsEncoding));
                }
                return encodedParams;
            } catch (UnsupportedEncodingException uee) {
                throw new RuntimeException("Encoding not supported: "
                        + paramsEncoding, uee);
            }
        }
        return null;
    }
}
